package com.example.fixedassets;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
//import android.provider.SyncStateContract.Constants;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.fixedassets.DatabaseManager;
import com.example.fixedassets.ItemList;

public class MainActivity extends Activity {
    ListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  DatabaseManager.getInstance().resetTables();
        //DatabaseManager.getInstance().resetTables();
       //DatabaseManager.getInstance().resetTables();
        DatabaseManager.init(this);
       
        
        ViewGroup contentView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_main, null);
        listView = (ListView) contentView.findViewById(R.id.list_view);
        
        Button btn = (Button) contentView.findViewById(R.id.button_add);
        setupButton(btn);
        setContentView(contentView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        
        setupListView(listView);
    }

    private void setupListView(ListView lv) {
        final List<ItemList> itemLists = DatabaseManager.getInstance().getAllItemLists();
        
        List<String> titles = new ArrayList<String>();
        for (ItemList wl : itemLists) {
            titles.add(wl.getName());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles);
        lv.setAdapter(adapter);

        final Activity activity = this;
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ItemList itemList = itemLists.get(position);
                Intent intent = new Intent(activity, ChoseItemListActivity.class);
             intent.putExtra(Constants.keyItemListId, itemList.getId());
               // intent.putExtra(Constants., itemList.getId());
                startActivity(intent);
            }
        });
    }
    
    private void setupButton(Button btn) {
        final Activity activity = this;
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(activity,AddItemListActivity.class);
                                
                startActivity (intent);
                
            }
        });
    }
}